package com.eleven.mall.service.impl;

import com.eleven.mall.customexception.BusinessException;
import com.eleven.mall.mapper.OrderMapper;
import com.eleven.mall.pojo.ExType;
import com.eleven.mall.pojo.OrderOut;
import com.eleven.mall.pojo.OrderPage;
import com.eleven.mall.pojo.entity.Order;
import com.eleven.mall.pojo.entity.OrderExample;
import com.eleven.mall.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class OrderServiceImpl implements OrderService {
    OrderMapper orderMapper;

//    得到订单列表
    @Override
    public OrderPage getOrderList(String query, String pagenum, Integer pagesize){
        OrderPage orderPage = new OrderPage();
//        OrderExample orderExample = new OrderExample();
        PageHelper.startPage(Integer.parseInt(pagenum), pagesize);

        List<Order> orderList = orderMapper.getOrderList(query);

        if (orderList == null || orderList.isEmpty()){
            throw new BusinessException("managerList not found", ExType.BussRecordNotFound);
        }

        ArrayList<OrderOut> orderOuts = new ArrayList<>();
        //4、遍历 orderList 存到order集合中作为响应的用户列表数据
        for(Order o : orderList) {
            OrderOut order = new OrderOut();
            order.setOrder_id(o.getOrderId());
            order.setUser_id(o.getUserId());
            order.setOrder_number(o.getOrderNumber());
            order.setOrder_price(o.getOrderPrice());
            order.setOrder_pay(o.getOrderPay());
            order.setIs_send(o.getIsSend());
            order.setTrade_no(o.getTradeNo());
            order.setOrder_fapiao_title(o.getOrderFapiaoTitle());
            order.setOrder_fapiao_company(o.getOrderFapiaoCompany());
            order.setOrder_fapiao_content(o.getOrderFapiaoContent());
            order.setConsignee_addr(o.getConsigneeAddr());
            order.setPay_status(o.getPayStatus());
            order.setCreate_time(o.getCreateTime());
            order.setUpdate_time(o.getUpdateTime());
            orderOuts.add(order);
        }

        //6、将要返回给控制层的数据存储到 orderPage
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        orderPage.setTotal((int) pageInfo.getTotal());
        orderPage.setPagenum(pageInfo.getPageNum());
        orderPage.setGoods(orderOuts);

        return orderPage;
    }
}
